/**
 * 必填
 */
function required(message = '此项必填', trigger = 'blur') {
  return {
    required: true,
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/^\s*$/g)
      if (reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
    message,
  }
}
/**
 * 不能为空
 */
function space(message = '不能为空', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value.trim() === '') {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 数字
 */
function number(message = '只能输入数字', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value === '') {
        callback(undefined)
      }
      const reg = new RegExp(/^[0-9]+$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 数字且大小在1-19
 */
function minNumber(message = '只能输入数字且大小在1-19之间', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value === '') {
        callback(undefined)
      }
      const reg = new RegExp(/^([1-9]|1\d)$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 浮点型数字
 */
function double(message = '只能是数字', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/[0-9]+$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 数字限制最大长度
 */
function numberLim(message = '数字限制最大长度', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/^[0-9]{0,5}$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 只能输入英文或数字
 */
function string(message = '只能输入英文或数字', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/^[a-zA-Z0-9]+$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 只能小写字母或者数字
 */
function NumAndStr(message = '只能是小写字母和数字', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/^[a-z0-9]+$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 中文数据验证 --ASCII码
 */
function checkChinese(message = '不能含有汉字', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      let flag = true
      if (value && value !== null) {
        for (let i = 0; i < value.length; i++) {
          if (value.charCodeAt(i) > 255) {
            callback(new Error(message))
            flag = false
            break
          }
        }
      }
      if (flag) {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 身份证校验
 */
function isCardNo(message = '身份证格式不正确', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 邮箱地址
 */
function email(message = '请输入正确的邮箱地址', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value === '') {
        callback(undefined)
      }
      const reg = new RegExp(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 移动手机号
 */
function mobile(message = '请输入正确的手机号', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/^1\d{10}$/)
      if (!reg.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 固定电话
 */
function tel(message = '请输入正确的固定电话', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value === '') {
        callback(undefined)
      } else {
        const reg = new RegExp(/^0\d{2,3}-?\d{7,8}$/)
        if (!reg.test(value)) {
          callback(new Error(message))
        } else {
          callback(undefined)
        }
      }
    },
    trigger,
  }
}
/**
 * 邮箱地址或手机号码
 */
function emailOrMobile(message = '请输入正确的邮箱地址或手机号码', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value === '') {
        callback(undefined)
      }
      const regEmail = new RegExp(
        /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/,
      )
      const regMobole = new RegExp(/^1\d{10}$/)

      if (regEmail.test(value) && regMobole.test(value)) {
        callback(undefined)
      } else {
        callback(new Error(message))
      }
    },
    trigger,
  }
}
/**
 * 座机号或手机号码
 */
function mobileOrTel(message = '请输入正确的座机号或手机号码', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      if (value === '') {
        callback(undefined)
      }
      const regMobole = new RegExp(/^1\d{10}$/)
      const regTel = new RegExp(/^0\d{2,3}-?\d{7,8}$/)

      if (regMobole.test(value) && regTel.test(value)) {
        callback(undefined)
      } else {
        callback(new Error(message))
      }
    },
    trigger,
  }
}
/**
 * 6位手机验证码
 */
function verificationCode(message = '请输入6位数字验证码', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp(/\d{6}$/)
      if (!reg.test(value)) {
        return callback(new Error(message))
      }
      callback(undefined)
    },
    trigger,
  }
}
/**
 * URL格式验证
 */
function isURL(message = 'URL格式不正确', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      const reg = new RegExp('^(http://|https://|/).*$')
      if (!reg.test(value) && value.trim() !== '') {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}
/**
 * 密码校验，及复杂度校验
 */
function pswd(message = '您输入的密码不满足要求', trigger = 'blur') {
  let grade
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      // 密码中必须包含大小字母、数字、特称字符，至少8个字符，最多16个字符。
      const reg1 = new RegExp('(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,16}')
      // 密码中必须包含字母、数字、特称字符，至少8个字符，最多16个字符。
      const reg2 = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}')
      // 密码中必须包含字母、数字，至少8个字符，最多16个字符。
      const reg3 = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,16}')

      if (reg1.test(value)) {
        grade = 1
      } else if (reg2.test(value)) {
        grade = 2
      } else if (reg3.test(value)) {
        grade = 3
      } else {
        grade = 0
      }
      if (value === '' || value === undefined) {
        callback(undefined)
      } else if (!reg3.test(value)) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
    grade: grade,
  }
}

//这里就是整个checkName啦，就是方法一的使用
function checkName(message = '主菜单名字不多于4个汉字或8个字母', trigger = 'blur') {
  return {
    validator: (_rule: unknown, value: string, callback: (arg0: Error | undefined) => void) => {
      let len = 0
      for (let i = 0; i < value.length; i++) {
        const c = value.charCodeAt(i)
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
          len++
        } else {
          len += 2
        }
      }
      if (len > 8) {
        callback(new Error(message))
      } else {
        callback(undefined)
      }
    },
    trigger,
  }
}

export default {
  required,
  number,
  space,
  minNumber,
  double,
  numberLim,
  string,
  NumAndStr,
  isCardNo,
  email,
  mobile,
  tel,
  emailOrMobile,
  mobileOrTel,
  verificationCode,
  checkChinese,
  isURL,
  pswd,
  checkName,
}
